package cn.yr.test.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
* @Title: Client
* @Description: socket客户端测试
* @author Lang
* @date 2020/5/17 11:23
*/

public class Client {

	public static void main(String[] args) throws Exception {
		Socket socket = new Socket();
		/*超时时间*/
		socket.setSoTimeout(30000);

		/*设置连接 连接ip和端口*/
		socket.connect(new InetSocketAddress(InetAddress.getLocalHost(),2000));

		System.out.println("连接已经建立");
		System.out.println(socket.getInetAddress()+"///"+socket.getPort());
		doto(socket);
		socket.close();
	}

	public  static void doto(Socket client)throws Exception {

		/* 获取输入流*/
		InputStream in = System.in;
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

		/* 获取输出流*/
		OutputStream outputStream = client.getOutputStream();
		PrintStream printStream = new PrintStream(outputStream);
		do{
			String s = bufferedReader.readLine();

			/*发送服务器*/
			printStream.println(s);
			System.out.println(s);

			/* 接受服务器*/
			String s1 = bufferedReader.readLine();
			System.out.println(s1);



		}while (true);

	}
}
